// JavaScript Document
var refreshTime=300   //刷新地址时间，单位（秒）
var _interFace=null;

var tmp_window_onload=window.onload;
window.onload = getLatLngIsLoad;

function getLatLngIsLoad() {
    if (typeof (tmp_window_onload) == "function") tmp_window_onload();
    include_js("http://maps.google.com/maps/api/js?sensor=true&language=zh-CN&region=GB&callback=getLatLng");
}

function getLatLng() {
    if(isPcSystem()==true){
        submitLatLng(0,0,"null",-1);
    }
    else{
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position){
                 submitLatLng(position.coords.latitude,position.coords.longitude,"null",1);
			}, 
			function(){
			    submitLatLng(0,0,"null",0);
			})
        }
        else if (google.gears) {
            var geo = google.gears.factory.create('beta.geolocation'); 
            geo.getCurrentPosition(function(position) {
               submitLatLng(position.coords.latitude,position.coords.longitude,"null",1);
            },
            function(){
                submitLatLng(0,0,"null",0);
            })
        }
        else {
            submitLatLng(0,0,"null",0)
        }   
   }
   setTimeout(getLatLng,refreshTime*1000);
}
function isPcSystem(){
    var pcSystem=['Win32','MacPPC','Linuxi586'];
    var platform=window.navigator.platform;
    for(var i=0;i<pcSystem.length && typeof(platform)!="undefined";i++){
        if(platform==pcSystem[i]){
            return true;
        }
    }
    return false;
}

//function getLatLngFromIp(type){
//    submitLatLng(0,0,"null",type);
//    include_js("http://j.maxmind.com/app/geoip.js",function(){
//        latLngToAddr(geoip_latitude(),geoip_longitude(),type)
//    });
//}
function submitLatLng(lat,lng,address,type){   
	var url="/tools/ajax.aspx?type=refurbgps";
	url=url + "&lat=" + lat + "&lon=" + lng + "&isgps=" + type + "&addr=" + address;
	var xmlHttp=GetXmlHttpObject();
	xmlHttp.open("GET",encodeURI(url),true);
	xmlHttp.setRequestHeader("If-Modified-Since", "0");
	//xmlHttp.onreadystatechange=function(){};
	xmlHttp.send(null);
}
function GetXmlHttpObject(){
  var xmlHttp=null;
  try{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
  catch (e){
	// Internet Explorer
	try{
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	catch (e){
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
  return xmlHttp;
}
/*window.onerror=function (msg,url,l){
	var txt="this Page hava a error \r\n\r\n";
	txt+="errorMsg:" + msg + "\r\n";
	txt+="URL: " + url + "\r\n";
	txt+="row:" + l + "\r\n";
	alert(txt);
	return true;
}*/